<?php

namespace App\Kuafu\Base\Validate;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class BaseValidate
{

    /**
     * 校验字段映射
     *
     * @var array
     */
    public static array $attributes = [];

    // 校验
    protected static function validateCheck($data, $rule, $message, $attributes = []): string
    {
        $validator = Validator::make($data, $rule, $message, $attributes);

        if ($validator->fails()) {
            $messages = [];
            foreach ($validator->errors()->toArray() as $k => $arr) {
                foreach ($arr as $item) {
                    $messages[] = $item;
                }
            }
            return implode(';', $messages);
        }

        return '';
    }

    /**
     * 一些共用的参数校验
     * @param string $param
     * @return string
     */
    protected static function commonRules(string $param): string
    {
        return [
                'pageSize' => 'nullable|integer|min:1|max:200',
                'pageNum'  => 'nullable|integer|min:1',
            ][$param] ?? '';
    }

    /**
     * 获取校验参数列表
     *
     * @param array $requestParams
     * @param array $ruleKeys
     * @param \Closure|null $closure
     * @return array
     */
    public static function getRequestParams(array $requestParams, array $ruleKeys, \Closure $closure = null): array
    {
        $realParams = [];
        //获取验证参数 & 公有处理参数
        foreach ($requestParams as $field => $value) {
            if (in_array($field, $ruleKeys)) {
                if (Str::endsWith(strtoupper($field), 'IMG')) {
                    $realParams[$field] = str_replace(config('other.IMAGE_URL'), '', $value);
                } else {
                    $realParams[$field] = $value;
                }
            }
        }
        //特殊参数闭包特殊处理
        if ($closure instanceof \Closure) {
            return $closure($realParams);
        }
        return $realParams;
    }

    /**
     * 基础校验 & 参数获取
     *
     * @param $params
     * @param $validateFields
     * @param array $validateMsg
     * @param \Closure|null $resolveParams
     * @return array
     */
    public static function baseValidate(
        $params,
        $validateFields,
        array $validateMsg = [],
        \Closure $resolveParams = null
    ): array {
        $validateRules = static::getRules($validateFields);
        return [
            $errMsg = self::validateCheck($params, $validateRules, $validateMsg, static::$attributes),//校验数据
            $errMsg ? [] : self::getRequestParams($params, array_keys($validateRules), $resolveParams)//获取请求校验数据并处理
        ];
    }

    /**
     * 获取校验参数规则
     *
     * @param array $rules
     * @param array $fields
     * @return array|mixed
     */
    public static function getActuallyRules(array $rules = [], array $fields = []): array
    {
        return empty($fields) ? $rules : (new Collection($rules))->filter(function ($v, $k) use ($fields) {
            if (in_array($k, $fields)) {
                return true;
            }
            return false;
        })->toArray();
    }
}
